<template>

  <div class="container">
    <div class="controls">
    <el-select v-model="selectedCamera" placeholder="请选择机位" style="width: 150px">
      <el-option label="机位 1" value="1"></el-option>
      <el-option label="机位 2" value="2"></el-option>
      <el-option label="机位 3" value="3"></el-option>
      <el-option label="机位 4" value="4"></el-option>
    </el-select>
    <el-button type="primary" round class="submit-btn" @click="submitCameraSelection">提交</el-button>
    </div>
<!--    <div class="video-container">-->
<!--      <video ref="video" :src="videoSrc2" autoplay playsinline muted></video>-->
<!--      <img class="video-stream" :src="videoSrc2" />-->
<!--    </div>-->
<div>
    <div v-if="isVideoAvailable" class="video-wrapper">
      <img class="video-stream" :src="videoSrc2" />
    </div>
    <div v-else class="video-container">
      <div >
        <div class="loader"></div>
        <h2 style="color: white">Loading...</h2>
      </div>
    </div>
</div>
    <div class="contr">
    <el-button type="primary" round class="refresh-btn" @click="refreshVideos">刷新</el-button>
    <el-button type="primary" round class="save-btn" @click="saveTrackPhoto">保存路线</el-button>
    </div>
  </div>
</template>


<script setup lang="ts">
import { ref ,onMounted} from 'vue';
import { trackClick,trackSaveClick } from "@/api/video";
import { ElSelect, ElOption, ElButton } from 'element-plus'
import { videoTrack1, videoTrack2, videoTrack3, videoTrack4 } from "@/api/types/multiplyVideo/trackVideo";
import { invade_close_click, monitor_close_click, register_close_click, track_close_click } from "@/api/close";
import { globalVariable } from '@/store/modules/globalurl'
/**
 * 钩子函数实时调用
 */
onMounted(() => {
  close(); // 调用你的函数
  checkVideoAvailability();
});

const isVideoAvailable = ref(false);

const checkVideoAvailability = async () => {
  try {
    const response = await fetch(videoSrc2.value);
    if (response.status === 200) {
      isVideoAvailable.value = true;
    } else {
      console.error('获取视频1信息失败，状态码：', response.status);
    }
  } catch (error) {
    console.error('发生错误：', error);
  }
};

function close(){

  register_close_click().then((response) => {

    var res = response.data
    if (res.respCode === '000000') {
      //ElMessage.success('关闭成功');

    } else {
      ElMessage.error(res.respMsg);
    }
  })
      .catch((error) => {
        console.error(error);
        // ElMessage.error('关闭失败，请重试');
      });
  invade_close_click().then((response) => {

    var res = response.data
    if (res.respCode === '000000') {
      //ElMessage.success('关闭成功');

    } else {
      ElMessage.error(res.respMsg);
    }
  })
      .catch((error) => {
        console.error(error);
        // ElMessage.error('关闭失败，请重试');
      });
  monitor_close_click().then((response) => {

    var res = response.data
    if (res.respCode === '000000') {
      //ElMessage.success('关闭成功');

    } else {
      ElMessage.error(res.respMsg);
    }
  })
      .catch((error) => {
        console.error(error);
        // ElMessage.error('关闭失败，请重试');
      });

}
//----------------------初始视频流-----------------------------
const videoSrc2 = ref(globalVariable+'/api/video_track/1');

const showCameraSelection = ref(false);
const selectedCamera = ref('');
function saveTrackPhoto(){

  trackSaveClick().then((response) => {

    var res = response.data
    if (res.respCode === '000000') {
      ElMessage.success('保存成功');

    } else {
      ElMessage.error(res.respMsg);
    }
  })
      .catch((error) => {
        console.error(error);
        ElMessage.error('保存失败，请重试');
      });

}
/**
 * 刷新路线追踪界面
 */
function refreshVideos() {

  trackClick()  // pass requestData object instead of function
      .then((response) => {

        var res = response.data
        if (res.respCode === '000000') {
          ElMessage.success('刷新成功');

        } else {
          ElMessage.error(res.respMsg);
        }
      })
      .catch((error) => {
        console.error(error);
        ElMessage.error('刷新失败，请重试');
      });
}

/**
 * 摄像头机位
 */
function toggleCameraSelection() {
  showCameraSelection.value = !showCameraSelection.value;
  selectedCamera.value = '';
}

function submitCameraSelection() {

  let selectedCameraValue = selectedCamera.value; // 从 <select> 元素中获取选中的值

  if (selectedCameraValue === '1') {
    // videoTrack1()
    videoSrc2.value = globalVariable+'/api/video_track/1';
    showCameraSelection.value = false;
    selectedCamera.value = '';
  }else if(selectedCameraValue === '2'){
    // videoTrack2()
    videoSrc2.value = globalVariable+'/api/video_track/2';
    showCameraSelection.value = false;
    selectedCamera.value = '';
  }else if(selectedCameraValue === '3'){
    // videoTrack3()
    videoSrc2.value = globalVariable+'/api/video_track/3';
    showCameraSelection.value = false;
    selectedCamera.value = '';
  }else if(selectedCameraValue === '4'){
    //videoTrack4()
    videoSrc2.value = globalVariable+'/api/video_track/4';
    showCameraSelection.value = false;
    selectedCamera.value = '';
  }
}
</script>

<style scoped>
.controls{
//margin-top: 30px;
  display: flex;
  justify-content: center;
  align-items: center;
//margin-bottom: 50px;
}

.contr {

}

.video-background {
  background-image: url("@/assets/background1.jpg");
  background-size: cover;
  background-position: center;
  height: 100vh;
}

.container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-color: transparent;
}

.video-container {
  display: grid;
  place-items: center;
  width: 800px;
  height: 450px;
  margin-top: 10px;
  margin-bottom: 16px;
  background-color: #3c5287;
}

.container {
  display: grid;
  place-items: center;
  height: 75vh;
  padding-top: 10px;
}

.button-container {
  display: flex;
  justify-content: center;
}

.submit-btn {
  font-size: 16px;
  height: 40px;
  width: 120px;
}

.submit-btn {
  margin-left: 16px;
}

.video-stream {
  width: 100%;
  height: 350px;
}

.loader {
  -webkit-animation: spin 1s linear infinite;
  animation: spin 1s linear infinite;
  border: 30px solid #ddd;
  border-bottom: 30px solid #64b5f6;
  border-radius: 50%;
  width: 100px;
  height: 100px;
}

@-webkit-keyframes spin {
  0% {
    -webkit-transform: rotate(0deg);
  }
  100% {
    -webkit-transform: rotate(-360deg);
  }
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(-360deg);
  }
}

h2 {
  animation: typing 1s infinite;
  font-size: 30px;
  margin-top: 30px;
  overflow: hidden;
}

@keyframes typing {
  from {
    width: 70%;
  }
  to {
    width: 100%;
  }
}

</style>